package cc.jq1024.middleware.encrypt.util;

/**
 * 类型转换工具
 * @author li--jiaqiang
 */
public class ConvertUtils {

    public static String byte2HexStr(byte[] bytes) {
        StringBuilder hexValue = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0'+ hex;
            }
            hexValue.append(hex.toUpperCase());
        }
        return hexValue.toString();
    }

    public static byte[] hexStr2Byte(String hexStr) {
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
    
}
